package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.repository.IStrategyCommentRepository;
import cn.wolfcode.luowowo.common.domain.StrategyComment;
import cn.wolfcode.luowowo.common.query.StrategyCommentObject;
import cn.wolfcode.luowowo.common.service.IStrategyCommentService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private IStrategyCommentRepository repository;

    @Autowired
    private MongoTemplate template;

    @Reference
    private IStrategyCommentService strategyCommentService;


    @Override
    public void saveOrUpdate(StrategyComment strategyComment) {
        if(!StringUtils.hasLength(strategyComment.getId())){
            //设置创建时间
            strategyComment.setCreateTime(new Date());
        }
        repository.save(strategyComment);
    }

    @Override
    public Page query(StrategyCommentObject qo) {
        //查询条件
        Query query = new Query();

        //攻略id条件(相当于SQL里的where detailId = #{detailId})
        if(qo.getDetailId()!= -1){
            query.addCriteria(Criteria.where("detailId").is(qo.getDetailId()));
        }

        //查总数
        long count = template.count(query, StrategyComment.class);

        if(count==0){
            return Page.empty();
        }

        //参数1:当前页  注意从0开始
        //参数2:每页显示条数
        //参数3:排序规则  第一个参数表示倒序  第二个参数表示按哪个字段排序
        PageRequest request = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.by(Sort.Direction.DESC, "createTime"));
        query.with(request);

        List<StrategyComment> list = template.find(query, StrategyComment.class);

        return new PageImpl(list,request,count);
    }

    @Override
    public void thumbUp(String toid, Long userId) {
        Optional<StrategyComment> op = repository.findById(toid);
        StrategyComment comment = op.get();
        if(comment!=null){
            List<Long> list = comment.getThumbuplist();
            if (list.contains(userId)) {
                //集合里已经有了的话表示取消点赞
                comment.setThumbupnum(comment.getThumbupnum()-1);
                //移除已点赞id集合里的用户id
                list.remove(userId);
            }else {
                //集合里没有的话表示点赞操作
                comment.setThumbupnum(comment.getThumbupnum()+1);
                //把用户id添加在已点赞id集合里
                list.add(userId);
            }
        }
        //更新操作
        this.saveOrUpdate(comment);
    }
}
